﻿using Cms.Application.Admin.Views;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class UserGroupsPresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly IUserGroupsView _view;

        public UserGroupsPresenter(IUserGroupsView view)
            : this(view, new RepositoryFactory())
        {
        }
        
        public UserGroupsPresenter(IUserGroupsView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            if (!_administrator.HasPermission.UserGroups)
                _view.RedirectToPermissionDeniedPage();
        }
        
        public void Initialize()
        {
            SetGroups();
        }

        public void SetGroups()
        {
            _view.SetGroups(UserRepository.GetUserGroups());
        }
        
        public void DeleteGroup(int id)
        {
            UserGroup group = UserRepository.GetUserGroup(id);
            // Clear references to pages
            foreach (Page page in group.SelectedPages)
                page.RequiredUserGroups.Remove(group);
            group.SelectedPages.Clear();
            foreach (Page page in group.DeselectedPages)
                page.DeselectedUserGroups.Remove(group);
            group.DeselectedPages.Clear();
            // Clear references to folders
            foreach (Folder folder in group.SelectedFolders)
                folder.RequiredUserGroups.Remove(group);
            group.SelectedFolders.Clear();
            foreach (Folder folder in group.DeselectedFolders)
                folder.DeselectedUserGroups.Remove(group);
            group.DeselectedFolders.Clear();
            // Clear references to mediafiles
            foreach (MediaFile file in group.SelectedMediaFiles)
                file.RequiredUserGroups.Remove(group);
            group.SelectedMediaFiles.Clear();
            foreach (MediaFile file in group.DeselectedMediaFiles)
                file.DeselectedUserGroups.Remove(group);
            group.DeselectedMediaFiles.Clear();
            // Clear references to mediafolders
            foreach (MediaFolder folder in group.SelectedMediaFolders)
                folder.RequiredUserGroups.Remove(group);
            group.SelectedMediaFolders.Clear();
            foreach (MediaFolder folder in group.DeselectedMediaFolders)
                folder.DeselectedUserGroups.Remove(group);
            group.DeselectedMediaFolders.Clear();
            // Clear references to content
            foreach (Content content in group.SelectedContent)
                content.RequiredUserGroups.Remove(group);
            group.SelectedContent.Clear();
            foreach (Content content in group.DeselectedContent)
                content.DeselectedUserGroups.Remove(group);
            group.DeselectedContent.Clear();
            // Save changes to remove references permanently
            UserRepository.SaveUserGroup(group);
            // Delete group
            UserRepository.DeleteUserGroup(id);
            SetGroups();
        }
    }
}